package com.bailugansan.servlet.user;

import com.alibaba.fastjson.JSONArray;
import com.bailugansan.pojo.User;
import com.bailugansan.service.UserService;
import com.bailugansan.service.implement.UserServiceImpl;
import com.bailugansan.util.Constants;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author BailuGansan
 */
public class PwdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");

        // Servlet 复用
        if(method != null && "check".equals(method)){
            checkPwd(req, resp);
        } else {
            pwdModify(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    // 修改密码
    protected void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String newpassword = req.getParameter("newpassword");

        if(o != null && !StringUtils.isNullOrEmpty(newpassword)){
            UserService userService = new UserServiceImpl();
            boolean updateResult = userService.pwdModify(((User) o).getId(), newpassword);
            if(updateResult){ // 成功则删除 Session 并退出登录
                req.setAttribute("message","修改成功，请重新登录");
                req.getSession().removeAttribute(Constants.USER_SESSION);
                try {
                    resp.sendRedirect(req.getContextPath()+"/login.jsp");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(111);
                req.setAttribute("message", "修改失败，请重试");
                resp.sendRedirect(req.getContextPath()+"/jsp/pwdmodify.jsp");
            }
        }
    }

    // 验证旧密码
    protected void checkPwd(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);
        String oldpassword = req.getParameter("oldpassword");

        Map<String, String> resultMap = new HashMap<String, String>();

        if(o == null ){ // Session 失效或者过期
            resultMap.put("result", "sessionerror");
        } else if(StringUtils.isNullOrEmpty(oldpassword)){ // 输入旧密码为空
            resultMap.put("result", "error");
        } else {
            String userPassword = ((User) o).getUserPassword();
            if(userPassword.equals(oldpassword)){ // 校验正确
                resultMap.put("result", "true");
            } else {
                resultMap.put("result", "false");
            }
        }

        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        writer.write(JSONArray.toJSONString(resultMap));
        writer.flush();
        writer.close();
    }
}
